package com.tuzhi.base.jpa.healper;

import com.tuzhi.base.jpa.domain.QueryConditionBO;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.math.NumberUtils;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.Date;

public class PredicateHelper {

    public static Predicate buildPredicate(QueryConditionBO queryConditon, Root root, CriteriaBuilder cb) {
        try {
            String name = queryConditon.getPropertyName();
            Class c = null;
            if (queryConditon.getObjValue() != null) {
                c = queryConditon.getObjValue().getClass();
            } else if (CollectionUtils.isNotEmpty(queryConditon.getObjList())) {
                c = queryConditon.getObjList().get(0).getClass();
            }

            Object objValue = queryConditon.getObjValue();
            switch (queryConditon.getQueryTypeEnum()) {
                case equal:
                    return cb.equal(root.get(name).as(c), objValue);
                case notEqual:
                    return cb.notEqual(root.get(name).as(c), objValue);
                case gt:
                    //大于
                    return cb.gt(root.get(name), NumberUtils.createNumber(objValue + ""));
                case ge:
                    //大于等于
                    return cb.ge(root.get(name), NumberUtils.createNumber(objValue + ""));
                case lt:
                    //小于
                    return cb.lt(root.get(name), NumberUtils.createNumber(objValue + ""));
                case le:
                    //小于等于
                    return cb.le(root.get(name), NumberUtils.createNumber(objValue + ""));
                case isEmpty:
                    //小于等于
                    return cb.isEmpty(root.get(name));
                case isNotEmpty:
                    //小于等于
                    return cb.isNotEmpty(root.get(name));
                case like:
                    return cb.like(root.get(name).as(c), "%"+objValue+"%");
                case notLike:
                    return cb.notLike(root.get(name).as(c), "%"+objValue+"%");
                case in:
                    CriteriaBuilder.In in = cb.in(root.get(name));
                    for (Object obj : queryConditon.getObjList()) {
                        in.value(obj);
                    }
                    return in;
                case notIn:
                    CriteriaBuilder.In notIn = cb.in(root.get(name));
                    for (Object obj : queryConditon.getObjList()) {
                        notIn.value(obj);
                    }
                    return cb.not(notIn);
                case between:
                    return cb.between(root.get(name), (Date)queryConditon.getObjList().get(0),
                            (Date)queryConditon.getObjList().get(1));
                case isNull:
                    return cb.isNull(root.get(name));
                case isNotNull:
                    return cb.isNotNull(root.get(name));
                default:
                    throw new IllegalArgumentException("参数查询类型错误!");
            }
        } catch (Exception e) {
            throw new IllegalArgumentException("buildPredicate错误!", e);
        }
    }

}
